<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Package_model extends CI_Model {

	function __construct() {
		parent::__construct();

		$this->load->library('LogSql');
	}

	/**
	 * 패키지 리스트
	 */
	function select_package_list() {
		log_message('debug',"#### Package_model::select_package_list()");

		$this->db->where_not_in('package_cd', array(PREM_GUEST, PREM_GENERAL));
		$query = $this->db->get('tb_package') or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 쿠폰 리스트
	 */
	function select_package_coupon_list($package_cd) {
		log_message('debug',"#### Package_model::select_package_coupon_list($package_cd)");

		$sql  = PHP_EOL." SELECT ";
		$sql .= PHP_EOL."       PC.* ";
		$sql .= PHP_EOL."     , (SELECT package_name FROM tb_package WHERE package_cd = PC.package_cd) package_name ";
		$sql .= PHP_EOL."     , (SELECT service_group_name FROM tb_service_group WHERE service_group_id = PC.service_group_id) service_group_name ";
		$sql .= PHP_EOL." FROM  tb_package_coupon PC ";
		$sql .= PHP_EOL." WHERE package_cd = ? ";
		$query = $this->db->query($sql, array($package_cd)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 패키지와 그에 속한 쿠폰 리스트
	 */
	function select_package_with_coupon_list() {
		log_message('debug',"#### Package_model::select_package_with_coupon_list()");
		
		$result = $this->select_package_list();
		
		for ($i=0; $i < count($result); $i++) { 
			$result[$i]['coupon'] = $this->select_package_coupon_list($result[$i]['package_cd']);
		}
		
		return $result;
	}
	
	/**
	 * 쿠폰 업데이트
	 */
	function update_package_coupon($package_cd, $service_group_id, $qty) {
		log_message('debug',"#### Package_model::update_package_coupon($package_cd, $service_group_id, $qty)");
		
		$this->db->set('update_dt', 'now()', FALSE);
		$this->db->set('qty'      , $qty);
		$this->db->where('package_cd'       , $package_cd);
		$this->db->where('service_group_id ', $service_group_id);
		$this->db->update('tb_package_coupon') or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}

}

/* End of file cashpoint_model.php */
/* Location: ./application/models/product/cashpoint_model.php */